import numpy as np
import cv2
from PIL import Image, ImageDraw, ImageFont


class MyDrawUtils:

    def __cv2ImgAddText(self, img, text, left, top, textColor=(0, 255, 0), textSize=30):
        if isinstance(img, np.ndarray):  # 判断是否OpenCV图片类型
            img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        # 创建一个可以在给定图像上绘图的对象
        draw = ImageDraw.Draw(img)
        # 字体的格式
        fontStyle = ImageFont.truetype(
            "font/simsun.ttc", textSize, encoding="utf-8")
        # 绘制文本
        draw.text((left, top), text, textColor, font=fontStyle, stroke_width=1)
        # 转换回OpenCV格式
        return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

    def drawText(self, mat, text, color):
        # 图片对象、文本、像素、字体、字体大小、颜色、字体粗细
        mat = self.__cv2ImgAddText(mat, text, 0, 10, color, 10)
        return mat
        # cv2.imshow("show", mat)

    def drawTextByFrontSize(self, mat, text, color, fontSize):
        # 图片对象、文本、像素、字体、字体大小、颜色、字体粗细
        mat = self.__cv2ImgAddText(mat, text, 0, 10, color, fontSize)
        return mat
        # cv2.imshow("show", mat)

    def drawText2(self, mat, x, y, text, color):
        # 图片对象、文本、像素、字体、字体大小、颜色、字体粗细
        mat = self.__cv2ImgAddText(mat, text, x, y, color, 20)
        return mat
        # cv2.imshow("show", mat)
